/**
 * Copyright By Grandsoft Company Limited.  
 * 2013-6-13 上午11:30:05
 */
package com.glodon.ggzy.jypt.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.glodon.ggzy.core.model.Page;
import com.glodon.ggzy.core.utils.Constants;
import com.glodon.ggzy.core.utils.JsonUtils;
import com.glodon.ggzy.core.utils.PageSearchModel;
import com.glodon.ggzy.entity.GgZfContract;
import com.glodon.ggzy.entity.GgZfOrder;
import com.glodon.ggzy.fwpt.service.IZfcgProjectService;
import com.glodon.ggzy.vo.GgZfContractVO;
import com.glodon.ggzy.vo.GgZfOrderVO;

/**
 * 
 * 供应商处理控制类
 * @author yangjf
 * @since jdk1.6
 * @date 2013-6-13
 *  
 */

@Controller
public class GysController {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IZfcgProjectService zfcgProjectService;

	/**
	 * 
	 * 供应商-主页
	 * @param model
	 * @return
	 */
	@RequestMapping("/jypt/gys_index")
	public String gysIndex(Model model, HttpServletRequest request) {
		String transNum = zfcgProjectService.getGysSuccessTransNumber(request);
		String amountNum = zfcgProjectService.getGysContractAmountNumber(request);
		model.addAttribute("transNum", transNum);
		model.addAttribute("amountNum", amountNum);
		return "/jypt/gys_index";
	}

	/**
	 * 供应商-公开招标
	 * @param model
	 * @return
	 */
	@RequestMapping("/jypt/gys_gkzb")
	public String gysGkzb(Model model) {
		return "/jypt/gys_gkzb";
	}

	/**
	 * 
	 * 我的订单
	 * @param model
	 * @return
	 */
	@RequestMapping("/jypt/gys_my_order")
	public String myOrder(Model model) {
		return "/jypt/gys_my_order";
	}

	/**
	 * 
	 * 我的合同
	 * @param model
	 * @return
	 */
	@RequestMapping("/jypt/gys_my_contract")
	public String myContract(Model model) {
		return "/jypt/gys_my_contract";
	}

	/**
	 * 
	 * 我的合同
	 * @param model
	 * @return
	 */
	@RequestMapping("/jypt/load_gys_my_contract_data")
	public void myContract(Model model, int rows, int page, String noOrName, HttpServletResponse response, HttpServletRequest request) {

		PageSearchModel queryModel = new PageSearchModel();
		queryModel.setPageNo(page);
		queryModel.setPageSize(rows);
		String jsonStr = "";

		Page queryPage = zfcgProjectService.queryContractPage(queryModel, Constants.ZF_GYS, noOrName, request);

		if (queryPage != null) {
			List<GgZfContract> gysList = (List<GgZfContract>) queryPage.getResult();
			List<GgZfContractVO> contractList = new ArrayList<GgZfContractVO>();
			for (GgZfContract gysContract : gysList) {
				GgZfContractVO ggZfContractVO = new GgZfContractVO();
				BeanUtils.copyProperties(gysContract, ggZfContractVO);
				contractList.add(ggZfContractVO);
			}
			queryPage.setResult(contractList);
			try {
				jsonStr = JsonUtils.toJsonStr(queryPage);
				response.getWriter().write(jsonStr);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 加载供应商数据
	 * @param model
	 * @param rows
	 * @param page
	 * @param noOrName
	 * @param response
	 * @param request
	 */
	@RequestMapping("/jypt/gys_my_order_data")
	public void loadGysOrderDataInfo(Model model, int rows, int page, String noOrName, HttpServletResponse response, HttpServletRequest request) {
		PageSearchModel psm = new PageSearchModel();
		psm.setPageNo(page);
		psm.setPageSize(rows);
		String jsonStr = "";
		Page queryPage = zfcgProjectService.queryOrderPage(psm, noOrName, false, request);
		if (queryPage != null) {
			List<GgZfOrder> orderList = (List<GgZfOrder>) queryPage.getResult();
			List<GgZfOrderVO> orderDataList = new ArrayList<GgZfOrderVO>();
			for (GgZfOrder order : orderList) {
				GgZfOrderVO ggZfOrderVO = new GgZfOrderVO();
				BeanUtils.copyProperties(order, ggZfOrderVO);
				orderDataList.add(ggZfOrderVO);
			}
			queryPage.setResult(orderDataList);
			try {
				jsonStr = JsonUtils.toJsonStr(queryPage);
				response.getWriter().write(jsonStr);
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

}
